﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;
namespace Maticsoft.DAL
{
    //listfrontend
    public partial class listfrontend
    {

        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from listfrontend");
            strSql.Append(" where ");
            strSql.Append(" ID = @ID  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Maticsoft.Model.listfrontend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into listfrontend(");
            strSql.Append("vendor,bitPart,deviceId,valueMapping,code,message,stationNumber,timestamp,displayTime,electricityCosume,accumulateVolume,count");
            strSql.Append(") values (");
            strSql.Append("@vendor,@bitPart,@deviceId,@valueMapping,@code,@message,@stationNumber,@timestamp,@displayTime,@electricityCosume,@accumulateVolume,@count");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                        new SqlParameter("@vendor", SqlDbType.NVarChar,50) ,
                        new SqlParameter("@bitPart", SqlDbType.NVarChar,50) ,
                        new SqlParameter("@deviceId", SqlDbType.NVarChar,200) ,
                        new SqlParameter("@valueMapping", SqlDbType.NVarChar,-1) ,
                        new SqlParameter("@code", SqlDbType.Int,4) ,
                        new SqlParameter("@message", SqlDbType.NVarChar,500) ,
                        new SqlParameter("@stationNumber", SqlDbType.Int,4) ,
                        new SqlParameter("@timestamp", SqlDbType.BigInt,8) ,
                        new SqlParameter("@displayTime", SqlDbType.DateTime) ,
                        new SqlParameter("@electricityCosume", SqlDbType.Decimal,9) ,
                        new SqlParameter("@accumulateVolume", SqlDbType.Decimal,9) ,
                        new SqlParameter("@count", SqlDbType.Int,4)

            };

            parameters[0].Value = model.vendor;
            parameters[1].Value = model.bitPart;
            parameters[2].Value = model.deviceId;
            parameters[3].Value = model.valueMapping;
            parameters[4].Value = model.code;
            parameters[5].Value = model.message;
            parameters[6].Value = model.stationNumber;
            parameters[7].Value = model.timestamp;
            parameters[8].Value = model.displayTime;
            parameters[9].Value = model.electricityCosume;
            parameters[10].Value = model.accumulateVolume;
            parameters[11].Value = model.count;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.listfrontend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update listfrontend set ");

            strSql.Append(" vendor = @vendor , ");
            strSql.Append(" bitPart = @bitPart , ");
            strSql.Append(" deviceId = @deviceId , ");
            strSql.Append(" valueMapping = @valueMapping , ");
            strSql.Append(" code = @code , ");
            strSql.Append(" message = @message , ");
            strSql.Append(" stationNumber = @stationNumber , ");
            strSql.Append(" timestamp = @timestamp , ");
            strSql.Append(" displayTime = @displayTime , ");
            strSql.Append(" electricityCosume = @electricityCosume , ");
            strSql.Append(" accumulateVolume = @accumulateVolume , ");
            strSql.Append(" count = @count  ");
            strSql.Append(" where ID=@ID ");

            SqlParameter[] parameters = {
                        new SqlParameter("@ID", SqlDbType.Int,4) ,
                        new SqlParameter("@vendor", SqlDbType.NVarChar,50) ,
                        new SqlParameter("@bitPart", SqlDbType.NVarChar,50) ,
                        new SqlParameter("@deviceId", SqlDbType.NVarChar,200) ,
                        new SqlParameter("@valueMapping", SqlDbType.NVarChar,-1) ,
                        new SqlParameter("@code", SqlDbType.Int,4) ,
                        new SqlParameter("@message", SqlDbType.NVarChar,500) ,
                        new SqlParameter("@stationNumber", SqlDbType.Int,4) ,
                        new SqlParameter("@timestamp", SqlDbType.BigInt,8) ,
                        new SqlParameter("@displayTime", SqlDbType.DateTime) ,
                        new SqlParameter("@electricityCosume", SqlDbType.Decimal,9) ,
                        new SqlParameter("@accumulateVolume", SqlDbType.Decimal,9) ,
                        new SqlParameter("@count", SqlDbType.Int,4)

            };

            parameters[0].Value = model.ID;
            parameters[1].Value = model.vendor;
            parameters[2].Value = model.bitPart;
            parameters[3].Value = model.deviceId;
            parameters[4].Value = model.valueMapping;
            parameters[5].Value = model.code;
            parameters[6].Value = model.message;
            parameters[7].Value = model.stationNumber;
            parameters[8].Value = model.timestamp;
            parameters[9].Value = model.displayTime;
            parameters[10].Value = model.electricityCosume;
            parameters[11].Value = model.accumulateVolume;
            parameters[12].Value = model.count;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string  datetime,int number )
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from listfrontend ");
            strSql.Append(" where CONVERT(VarChar(10), displayTime, 120) =   @displayTime   and  stationNumber =@stationNumber");
            SqlParameter[] parameters = {
                    new SqlParameter("@displayTime", SqlDbType.NVarChar,10),
                    new SqlParameter("@stationNumber", SqlDbType.Int)

            };
            parameters[0].Value = datetime;
            parameters[1].Value = number;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from listfrontend ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.listfrontend GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID, vendor, bitPart, deviceId, valueMapping, code, message, stationNumber, timestamp, displayTime, electricityCosume, accumulateVolume, count  ");
            strSql.Append("  from listfrontend ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;


            Maticsoft.Model.listfrontend model = new Maticsoft.Model.listfrontend();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.vendor = ds.Tables[0].Rows[0]["vendor"].ToString();
                model.bitPart = ds.Tables[0].Rows[0]["bitPart"].ToString();
                model.deviceId = ds.Tables[0].Rows[0]["deviceId"].ToString();
                model.valueMapping = ds.Tables[0].Rows[0]["valueMapping"].ToString();
                if (ds.Tables[0].Rows[0]["code"].ToString() != "")
                {
                    model.code = int.Parse(ds.Tables[0].Rows[0]["code"].ToString());
                }
                model.message = ds.Tables[0].Rows[0]["message"].ToString();
                if (ds.Tables[0].Rows[0]["stationNumber"].ToString() != "")
                {
                    model.stationNumber = int.Parse(ds.Tables[0].Rows[0]["stationNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["timestamp"].ToString() != "")
                {
                    model.timestamp = long.Parse(ds.Tables[0].Rows[0]["timestamp"].ToString());
                }
                if (ds.Tables[0].Rows[0]["displayTime"].ToString() != "")
                {
                    model.displayTime = DateTime.Parse(ds.Tables[0].Rows[0]["displayTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["electricityCosume"].ToString() != "")
                {
                    model.electricityCosume = decimal.Parse(ds.Tables[0].Rows[0]["electricityCosume"].ToString());
                }
                if (ds.Tables[0].Rows[0]["accumulateVolume"].ToString() != "")
                {
                    model.accumulateVolume = decimal.Parse(ds.Tables[0].Rows[0]["accumulateVolume"].ToString());
                }
                if (ds.Tables[0].Rows[0]["count"].ToString() != "")
                {
                    model.count = int.Parse(ds.Tables[0].Rows[0]["count"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM listfrontend ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM listfrontend ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


    }
}

